﻿using AntdUI;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeGenerator.Utils
{
    public static class CommonUtil
    {

        public static string GenFullPath(string inputPath)
        {
            string fullPath = inputPath;

            // 如果输入路径是相对路径，则将其与当前目录组合以获取绝对路径
            if (!Path.IsPathRooted(inputPath))
            {
#if DEBUG
                string current = Directory.GetCurrentDirectory();
                fullPath = Path.Combine(current, inputPath);
#else
                string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                fullPath = Path.Combine(appDataFolder, "CodeGenerator",inputPath);
#endif
            }

            return fullPath;
        }

        public static string ToSmallCamelCase(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return s;
            }
            //验证是否是小驼峰
            bool IsSmallCamelCase(string str)
            {
                if (string.IsNullOrEmpty(str) || !char.IsLower(str[0]))
                    return false;

                bool hasUpper = false;
                for (int i = 1; i < str.Length; i++)
                {
                    if (char.IsUpper(str[i]))
                    {
                        hasUpper = true;
                    }
                    else if (char.IsLower(str[i]) && hasUpper)
                    {
                        return true;
                    }
                }
                return false;
            }

            // 若已经是小驼峰直接返回
            if (IsSmallCamelCase(s))
            {
                return s;
            }
            StringBuilder result = new StringBuilder();
            bool nextCharToUpper = false;

            foreach (char c in s)
            {
                if (c == '_')
                {
                    nextCharToUpper = true;
                }
                else
                {
                    if (nextCharToUpper)
                    {
                        result.Append(char.ToUpper(c));
                        nextCharToUpper = false;
                    }
                    else
                    {
                        result.Append(char.ToLower(c));
                    }
                }
            }

            // Ensure the first character is lowercase
            if (result.Length > 0)
            {
                result[0] = char.ToLower(result[0]);
            }

            return result.ToString();
        }

        public static string ToBigCamelCase(this string s)
        {
            s = s.ToSmallCamelCase();
            if (s.Length > 1)
            {
                return char.ToUpper(s[0]) + s.Substring(1);
            }
            else
            {
                return char.ToUpper(s[0]) + "";
            }
        }
        public static bool IsLowerCamelFirstLongEnough(this string s)
        {
            var isSmallCamelCase = s.ToSmallCamelCase() == s;
            if (!isSmallCamelCase)
            {
                return false;
            }
            var cnt = 0;
            foreach (var c in s)
            {
                if (c >= 'a' && c <= 'z')
                {
                    cnt++;
                }
                if (c >= 'A' && c <= 'Z')
                {
                    if (cnt >= 2) return true;
                    return false;
                }
            }
            return true;
        }
        public static string FirstWord(this string s)
        {
            string pattern = @"^[a-zA-Z\u4e00-\u9fa5]+";
            // 创建 Regex 对象
            Regex regex = new Regex(pattern);
            var match = regex.Match(s);
            if (match.Success)
            {
                return match.Value;
            }
            return null;
        }
        public static string UpperFirst(this string s)
        {
            if (s.Length > 1)
            {
                return char.ToUpper(s[0]) + s.Substring(1);
            }
            else
            {
                return char.ToUpper(s[0]) + "";
            }
        }

        public static string LowerFirst(this string s)
        {
            if (s.Length > 1)
            {
                return char.ToLower(s[0]) + s.Substring(1);
            }
            else
            {
                return char.ToLower(s[0]) + "";
            }
        }

        public static bool IsNullOrEmpty(this string s)
        {
            return s == null || s.Length == 0;
        }

        public static bool IsJavaPackageName(this string input)
        {
            // 使用正则表达式来匹配Java包名规范
            string pattern = "^[a-z][a-z0-9_]*(\\.[a-z0-9_]+)*$";
            return Regex.IsMatch(input, pattern);
        }

        public static bool IsNotNullAndNotEmpty(this string s)
        {
            return s != null && s.Length > 0;
        }

    }
}